<?php
/*
* Class teia\webdev\php\core\
* Core 
*
* @author Bernardo Quibiana
* @date created: 20111122
*
* @date modified: 20111122
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\core;

require_once 'TypePool.inc';
require_once 'FunctionPool.inc';

class FileUtil {
	
	static function upload_display_form($action, $file_type, $method='POST', $max_file_size=1000000, $file_txt='Choose file', $submit_txt='Upload') {
		echo self::upload_generate_form($action, $file_type, $method, $max_file_size, $file_txt, $submit_txt);	
	}
	
	static function upload_generate_form($action, $file_type, $method='POST', $max_file_size=1000000, $file_txt='Choose file', $submit_txt='Upload') {
		$content = "<form action=\"$action\" method=\"$method\" enctype=\"multipart/form-data\">\n";
		$content .= "\t<div>\n";
		$content .= "\t\t<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$max_file_size\" />\n";
		$content .= "\t\t<input type=\"hidden\" name=\"file_type\" value=\"$file_type\" />\n";
		$content .= "\t\t<label for=\"_upload\">$file_txt</label>\n";
		$content .= "\t\t<input type=\"file\" name=\"_upload\" id=\"_upload\" />\n";
		$content .= "\t\t<input type=\"submit\" value=\"$submit_txt\" />\n";
		$content .= "\t</div>\n";
		$content .= "</form>\n";
		
		return $content;
	}
	
	static function upload_handle_form($save_dir, &$err_msg) {
		$file_err = $_FILES['_upload']['error'];
		$file_type = $_FILES['_upload']['type'];
		$file_tmp = $_FILES['_upload']['tmp_name'];
		$file_name = $_FILES['_upload']['name'];
		
		$exp_type = $_REQUEST['file_type'];
		if (!isset($err_msg)) $err_msg = '';
		
		if ($file_err > 0) {
			$err_msg = self::upload_error_msg($file_err);		
		} else {	
			if ($file_type == $exp_type) {
				if (is_uploaded_file($file_tmp)) {
					$pass = true;
					if (is_null($save_dir)) {
						return $file_tmp;
					} else {
						$save_path = $save_dir . $file_name;
						$pass = move_uploaded_file($file_tmp, $save_path);
						$err_msg = "Unable to save file '$file_name' to destinaton '$save_path'";
						return $pass ? $save_path : null;
					}
				} else {
					$err_msg = "Warning: File was not uploaded '$file_name'";	
				}
			} else {
				$err_msg = "Unexpected file type '$file_type', expected type '$exp_type'";
			}
		}
		return null;
	}
	
	static function upload_error_msg($err) {
		$err_msg = '';
		switch ($err) {
			case UPLOAD_ERR_OK: $err_msg = '';
			break;
			case UPLOAD_ERR_INI_SIZE: $err_msg = 'File exceeded maximum file size';
			break;
			case UPLOAD_ERR_FORM_SIZE: $err_msg = 'File exceeded maximum upload file size';
			break;
			case UPLOAD_ERR_PARTIAL: $err_msg = 'File was partially uploaded';
			break;
			case UPLOAD_ERR_NO_FILE: $err_msg = 'No file was uploaded';
			break;
			case UPLOAD_ERR_NO_TMP_DIR: $err_msg = 'No file was uploaded. Reason: no temporary directory was especfied';
			break;
			case UPLOAD_ERR_CANT_WRITE: $err_msg = 'No file was uploaded. Reason: no write rights';
			break;
			case UPLOAD_ERR_EXTENSION: $err_msg = 'Unexpected file type';
			break;
			default: $err_msg = 'Unknown upload error';
			break;
		}
		return $err_msg;
	}
	
	
	static function csv_parse_file($path, $mode='r', $length=0, $delimiter=',', $enclosure='"', $escape='\\') {
		$csv = null;
		if (($handle = fopen($path, $mode)) !== FALSE) {
			$csv = array();
			while (($row = fgetcsv($handle, $length, $delimiter, $enclosure, $escape)) !== FALSE) {
				array_push($csv, $row);
			}
			fclose($handle);
		}
		return $csv;
	}
}

?>